package com.pxmeta.party.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.pxmeta.party.entity.vo.AgoraUserVO;
import com.pxmeta.party.entity.vo.KickingVO;
import com.pxmeta.common.exception.ServiceException;
import com.pxmeta.user.converter.UserConverter;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.entity.dto.UserDTO;
import com.pxmeta.user.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Base64;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class AgoraService {

    private final RestTemplate restTemplate;
    private final IUserService userService;

    // 客户 ID
    private final static String customerKey = "Your customer key";
    // 客户密钥
    private final static String customerSecret = "Your customer secret";
    private final static String kickingRuleUrl = "https://api.agora.io/dev/v1/kicking-rule";
    //获取用户列表 url
    private final static String listUserUrl = "https://api.agora.io/dev/v1/channel/user/%s/%s";

    private HttpHeaders getHeader(){
        // 拼接客户 ID 和客户密钥并使用 base64 编码
        String plainCredentials = customerKey + ":" + customerSecret;
        String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes()));
        // 创建 authorization header
        String authorizationHeader = "Basic " + base64Credentials;
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setBearerAuth(authorizationHeader);
        return headers;
    }

    public void kickingRule(KickingVO vo){
        HttpHeaders header = getHeader();
        HttpEntity<Object> entity = new HttpEntity<>(vo,header);
        ResponseEntity<JSONObject> response = restTemplate.postForEntity(kickingRuleUrl, entity, JSONObject.class);
        if (response.getStatusCode() != HttpStatus.OK){
            throw new ServiceException("服务请求失败");
        }
        JSONObject body = response.getBody();
        if (!Objects.requireNonNull(body).get("status").equals("success")){
            throw new ServiceException("远程接口报错");
        }
        Integer id = body.getInteger("id");
        //todo 保存
    }


    public List<UserDTO> getUsers(String appId,String channelName) {
        HttpHeaders header = getHeader();
        String url = String.format(listUserUrl, appId, channelName);
        ResponseEntity<JSONObject> response = restTemplate.getForEntity(url, JSONObject.class, header);
        if (response.getStatusCode() != HttpStatus.OK){
            throw new ServiceException("服务请求失败");
        }
        JSONObject body = response.getBody();
        Boolean success = body.getBoolean("success");
        if (!success){
            throw new ServiceException("远程接口报错");
        }
        AgoraUserVO vo = body.getObject("data", AgoraUserVO.class);
        List<Long> userIds = vo.getUsers();
        if (CollectionUtil.isEmpty(userIds)){
            return Lists.newArrayList();
        }
        List<User> users = userService.listByIds(userIds);
        return UserConverter.toDtoList(users);

    }
}
